package com.itranswarp.learnjava;

/**
 * Learn Java from https://www.liaoxuefeng.com/
 * 
 * @author liaoxuefeng
 */
public class Main {

	public static void main(String[] args) {
		String[] fields = { "name", "position", "salary" };
		String table = "employee";
		String insert = buildInsertSql(table, fields);
		System.out.println(insert);
		System.out.println(
				"INSERT INTO employee (name, position, salary) VALUES (?, ?, ?)".equals(insert) ? "测试成功" : "测试失败");
	}

	static String buildInsertSql(String table, String[] fields) {
		// TODO:
		StringBuilder sb = new StringBuilder();
		sb.append("INSERT INTO ");
		sb.append(table);
		sb.append("(");
		for (int i = 0; i < fields.length; i++) {
			sb.append(fields[i]);
			
			if(i<2) {
				sb.append(",");
			}
		}
		sb.append(")");
		sb.append("VALUES");
		sb.append("(");
		for (int i = 0; i < fields.length; i++) {
			sb.append("?");
			
			if(i<2) {
				sb.append(",");
			}
		}
		sb.append(")");
		return sb.toString();
	}

}
